﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CsvHelper;
namespace BreadthFirst
{
    class Program
    {
        private static Dictionary<string, People> peopleDic = new Dictionary<string, People>();
        private static Queue<People> queue = new Queue<People>();
        static void Main(string[] args)
        {

            //全局HashMap

            #region 加载数据

            using (var sr = new StreamReader("Graph.csv"))
            {
                using (var csv = new CsvReader(sr))
                {
                    foreach (var item in csv.GetRecords<People>())
                    {
                        if (!peopleDic.ContainsKey(item.Name))
                            peopleDic.Add(item.Name, new People(item.Name, item.Neighbors, item.IsDealer));
                    }
                    Console.WriteLine($"Hello World! {peopleDic.Count}");
                }
            }

            //获取没有子节点的节点名字
            List<string> childrenNames = new List<string>();
            foreach (var item in peopleDic)
            {
                foreach (var neighborName in item.Value.Neighbors)
                {
                    if (!peopleDic.ContainsKey(neighborName))
                    {
                        childrenNames.Add(neighborName);
                    }
                }
            }

            //添加没有子节点的节点
            foreach (var childrenName in childrenNames)
            {
                if (!peopleDic.ContainsKey(childrenName))
                    peopleDic.Add(childrenName, new People(childrenName, new string[0], false));
            }

            #endregion 加载数据

            #region 写入数据

            List<People> peoples = new List<People>();
            peoples = peopleDic.Select(x => x.Value).ToList();


            using (var sr = new StreamWriter("Graph.csv"))
            {
                using (var csv = new CsvWriter(sr))
                {
                    csv.WriteRecords<People>(peoples.ToList());
                    Console.WriteLine($"Hello World! {peoples.Count}");
                }
            }

            #endregion

            #region 广度优先查找 
            string startName = "you";
            string result = Search(startName);
            Console.WriteLine($"result = {result}");

            #endregion

        }

        private static string Search(string name)
        {
            if (peopleDic.ContainsKey(name))
            {
                if (!peopleDic[name].Searched)
                {
                    if (peopleDic[name].IsDealer)
                        return name;
                    else
                    {
                        foreach (var childName in peopleDic[name].Neighbors)
                            queue.Enqueue(peopleDic[childName]);
                        peopleDic[name].Searched = true;
                        if (queue.Count > 0)
                            return Search(queue.Dequeue().Name);
                    }
                }
            }
            return "Not Found!";
        }

    }
}
